package com.itfreer.data.metadata;

import android.util.Log;

import com.itfreer.data.metadata.entity.DictionaryEntity;
import com.itfreer.data.metadata.entity.DirectoryEntity;
import com.itfreer.data.SysDatabaseHelper;
import com.itfreer.data.metadata.entity.MetadataEntity;
import com.itfreer.data.remote.DataService;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 定义元数据服务同步服务接口
 */
public class MetadataSynService {

    private static final String directorySN = "metadatadirectory";
    private static final String dictionarySN = "dictionary";
    private static final String metadataSN = "metadata";
    private static final Integer pageSize = 100;

    public static final MetadataSynService instance = new MetadataSynService();
    private MetadataSynService() {

    }

    /**
     * 同步目录
     * @return
     */
    public Boolean synDirectory() {
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(SysDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<DirectoryEntity, String> dao = SysDatabaseHelper.getInstance().getDao(DirectoryEntity.class);
                // 清空表
                dao.queryRaw("delete from directory");

                // 从远程获取表总数
                Integer rowCount = DataService.instance.getRowCounts(directorySN, null);
                Integer pageCount = (rowCount / pageSize) + (rowCount % pageSize > 0 ? 1 : 0);

                // 从远程获取表记录
                for(Integer pageIndex = 1;pageIndex<=pageCount;pageIndex++){
                    List<DirectoryEntity> datas = DataService.instance.getEntitys(directorySN,null,null,null, pageSize, pageIndex, DirectoryEntity.class);
                    if(datas!=null && datas.size()>0){
                        for(DirectoryEntity item : datas){
                            dao.create(item);
                        }
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 同步字典
     * @return
     */
    public Boolean synDictionary() {
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(SysDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<DictionaryEntity, String> dao = SysDatabaseHelper.getInstance().getDao(DictionaryEntity.class);
                // 清空表
                dao.queryRaw("delete from dictionary");

                // 从远程获取表总数
                Integer rowCount = DataService.instance.getRowCounts(dictionarySN, null);
                Integer pageCount = (rowCount / pageSize) + (rowCount % pageSize > 0 ? 1 : 0);

                // 从远程获取表记录
                for(Integer pageIndex = 1;pageIndex<=pageCount;pageIndex++){
                    List<DictionaryEntity> datas = DataService.instance.getEntitys(dictionarySN,null,null,null, pageSize, pageIndex, DictionaryEntity.class);
                    if(datas!=null && datas.size()>0){
                        for(DictionaryEntity item : datas){
                            dao.create(item);
                        }
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 同步元数据
     * @return
     */
    public Boolean synMetadata() {
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(SysDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<MetadataEntity, String> dao = SysDatabaseHelper.getInstance().getDao(MetadataEntity.class);
                // 清空表
                dao.queryRaw("delete from metadata");

                // 从远程获取表总数
                Integer rowCount = DataService.instance.getRowCounts(metadataSN, null);
                Integer pageCount = (rowCount / pageSize) + (rowCount % pageSize > 0 ? 1 : 0);

                // 从远程获取表记录
                for(Integer pageIndex = 1;pageIndex<=pageCount;pageIndex++){
                    List<MetadataEntity> datas = DataService.instance.getEntitys(metadataSN,null,null,null, pageSize, pageIndex, MetadataEntity.class);
                    if(datas!=null && datas.size()>0){
                        for(MetadataEntity item : datas){
                            dao.create(item);
                        }
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }
}
